package dao

import (
	"fmt"
	__ "hospital-srv/basic/proto"
	"hospital-srv/handler/models"
)

// 用户手动添加
func UserHealthDataCreated(in *__.UserHealthDataCreatedReq) (*models.HealthData, error) {
	var healthdata models.HealthData
	var user models.User
	user.Id = uint32(in.UserId)
	err := user.GetUserInfoById(int(in.UserId)) // 直接传入入参ID
	if err != nil {
		return nil, fmt.Errorf("id查找用户失败: %v", err)
	}
	if user.Id == 0 { // 此时，若查询不到，user.Id 会是0（需确保 GetUserInfoById 逻辑：查询失败时将 user.Id 置0）
		return nil, fmt.Errorf("用户不存在")
	}
	healthdata.UserId = uint(in.UserId)
	healthdata.EquipmentId = uint(in.EquipmentId)   //设备id
	healthdata.BodyTemperature = in.BodyTemperature //体温
	healthdata.BloodSugar = in.BloodSugar           //血糖
	healthdata.BloodPressure = in.BloodPressure     //血压收缩
	healthdata.BloodPressureD = in.BloodPressureD   //血压舒张
	healthdata.BloodOxygen = in.BloodOxygen         //血氧

	err = healthdata.CreateData()
	if err != nil {
		return nil, fmt.Errorf("添加数据失败")
	}
	return &healthdata, nil
}

// 用户查看健康数据
func GetUserHealthData(in *__.GetUserHealthDataReq) ([]*__.GetUserHealthData, error) {
	var healthData models.HealthData
	var healthDatalist []*__.GetUserHealthData
	healthData.UserId = uint(in.UserId)
	data, err := healthData.GetUserHealthData(int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, err
	}
	for _, health := range data {
		healthDatalist = append(healthDatalist, &__.GetUserHealthData{
			Id:              uint64(health.Id),
			UserId:          uint64(health.UserId),
			EquipmentId:     uint64(health.EquipmentId),
			BodyTemperature: health.BodyTemperature,
			BloodSugar:      health.BloodSugar,
			BloodPressure:   health.BloodPressureD,
			BloodPressureD:  health.BloodPressure,
			BloodOxygen:     health.BloodOxygen,
		})
	}
	return healthDatalist, nil
}
